package cn.jn.lhm.teacher.handler.front;

import cn.hutool.core.util.StrUtil;
import cn.jn.lhm.service.config.exception.GlobalException;
import cn.jn.lhm.teacher.constant.EduConstant;
import cn.jn.lhm.teacher.entity.EduComment;
import cn.jn.lhm.teacher.entity.EduCourse;
import cn.jn.lhm.teacher.entity.chapter.ChapterVo;
import cn.jn.lhm.teacher.entity.frontvo.CourseDataVo;
import cn.jn.lhm.teacher.entity.frontvo.CourseInfoFrontVo;
import cn.jn.lhm.teacher.feign.OrderClient;
import cn.jn.lhm.teacher.service.EduChapterService;
import cn.jn.lhm.teacher.service.EduCourseService;
import cn.jn.lhm.utils.JwtUtils;
import cn.jn.lhm.utils.R;
import cn.jn.lhm.utils.order.EduOrder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/13 - 21:00
 */
@RestController
@RequestMapping("/course/front")
public class IndexCourseHandler {


    @Resource
    private EduCourseService eduCourseService;


    @Resource
    private EduChapterService eduChapterService;

    @Resource
    private OrderClient orderClient;


    @PostMapping("/get/course/page/{current}/{limit}")
    public R getCourseListByPage(@PathVariable("current")Long current,
                                 @PathVariable("limit")Long limit,
                                 @RequestBody(required = false)CourseInfoFrontVo frontVo){
        Page<EduCourse> page = new Page<>(current, limit);
        Map<String, Object> map = eduCourseService.getCourseListByFrontCondition(page, frontVo);
        return R.ok().data(map);
    }


    @PostMapping("/add/comment")
    public R addComment(@RequestBody EduComment eduComment){
        eduCourseService.saveCommentInfo(eduComment);
        return R.ok();
    }


    @GetMapping("/get/comment/{id}/{current}/{limit}")
    public R getCommentInfoByCourseId(@PathVariable("id")String id ,
                                      @PathVariable("current")Long current,
                                      @PathVariable("limit")Long limit){
        Page<EduComment> page = new Page<>(current, limit);
        eduCourseService.getCommentInfoByCourseId(id, page);
        return R.ok().data("data", page.getRecords()).data("total", page.getTotal());
    }


    @GetMapping("/get/course/info/{id}")
    public R getCourseInfo(@PathVariable("id")String id, HttpServletRequest request){
        //根据课程ID查询课程的基本信息
        CourseDataVo courseDataVo = eduCourseService.getCourseAllInfo(id);
        //根据课程Id查询章节小节
        List<ChapterVo> chapterVoList = eduChapterService.getAll(id);
        //TODO 查询课程的支付状态，调用order接口
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        boolean status = false;
        if (!StrUtil.hasEmpty(userId)){
            status  = orderClient.getOrderInfoToCheckStatus(userId, id);
        }
        return R.ok().data("courseInfo", courseDataVo).data("chapterList", chapterVoList).data("status", status);
    }

    @GetMapping("/get/course/info/order/{id}")
    public EduOrder getCourseInfoToOrder(@PathVariable("id")String id){
        CourseDataVo dataVo = eduCourseService.getCourseAllInfo(id);
        EduOrder order = new EduOrder();
        BeanUtils.copyProperties(dataVo, order);
        return order;
    }

}
